import pako from 'pako';

// 将对象转换为一串短字符
export const objectToString = (obj) => {
  const jsonString = JSON.stringify(obj); // 转换为 JSON 字符串
  const compressed = pako.deflate(jsonString, { to: 'uint8array' }); // Gzip 压缩为字节数组
  const base64String = btoa(String.fromCharCode(...compressed)); // 转换为 Base64 字符串
  return base64String; // 返回结果
};

// 将字符串反解析为对象
export const stringToObject = (str) => {
  const compressed = Uint8Array.from(atob(str), (c) => c.charCodeAt(0)); // 解码为字节数组
  const jsonString = pako.inflate(compressed, { to: 'string' }); // 解压缩
  const obj = JSON.parse(jsonString); // 转换回对象
  return obj; // 返回结果
};
